<?php

$cat = new tools();
$cat->dbc = $tool->dbc;

/******** CAMBIE DESDE AQUI *************/

$queryCats = 'SELECT '
.'  cc1.id as idCategoria1, c.categoria1 '
.', cc2.id as idCategoria2, c.categoria2 '
.', cc3.id as idCategoria3, c.categoria3 '
.', cc4.id as idCategoria4, c.categoria4 '
.', cc5.id as idCategoria5, c.categoria5 '
.'FROM cliente c '
.'left join ( cliente_categoria cc1) ON (c.categoria1=cc1.nombre ) '
.'left join ( cliente_categoria cc2) ON (c.categoria2=cc2.nombre ) '
.'left join ( cliente_categoria cc3) ON (c.categoria3=cc3.nombre ) '
.'left join ( cliente_categoria cc4) ON (c.categoria4=cc4.nombre ) '
.'left join ( cliente_categoria cc5) ON (c.categoria5=cc5.nombre ) '
.'WHERE c.id='.$_SESSION['CLIENTE_ID'].' ';

if(empty($_SESSION['CLIENTE_ID'])){
    $extra1 = "where c.only_for = 0";
}else{
    $extra1 = "where c.only_for in (0,1";
    $extraCats = $cat->estructura_db($queryCats);

    $idCategoriaExtra1 = $extraCats[0]['idCategoria1'];
    $idCategoriaExtra2 = $extraCats[0]['idCategoria2'];
    $idCategoriaExtra3 = $extraCats[0]['idCategoria3'];
    $idCategoriaExtra4 = $extraCats[0]['idCategoria4'];
    $idCategoriaExtra5 = $extraCats[0]['idCategoria5'];

    if (!empty ($idCategoriaExtra1)) $extra1.= ', '.$idCategoriaExtra1;
    if (!empty ($idCategoriaExtra2)) $extra1.= ', '.$idCategoriaExtra2;
    if (!empty ($idCategoriaExtra3)) $extra1.= ', '.$idCategoriaExtra3;
    if (!empty ($idCategoriaExtra4)) $extra1.= ', '.$idCategoriaExtra4;
    if (!empty ($idCategoriaExtra5)) $extra1.= ', '.$idCategoriaExtra5;


    $extra1 .= " ) ";
}

/******** CAMBIE HASTA AQUI *************/
?>
<div class="menu">
  <ul class="menu-1">
<?

	// Productos

		$query = "select
					id,
					nombre,
					(
						select
							count(*)
						from
							prod_subcategoria where cat_id = c.id
					) as sub,
					(
						select
							count(*)
						from
							producto
						where
							cat_nivel = 1 and cat_id = c.id
					) as art
				 from prod_categoria c $extra1
				 order by orden";

		$cat1 = $cat->estructura_db($query);

	 for($c1=0;$c1<count($cat1);$c1++){ $NIVEL = 1; $CATID = $cat1[$c1]['id']; ?>

				<li><h2><?php echo utf8_encode($cat1[$c1]['nombre']); ?></h2>
                	<?php if(($cat1[$c1]['sub']+$cat1[$c1]['art']) > 0){ ?>
                        <ul class="menu-2">
                            <?php if($cat1[$c1]['sub']>0){ ///si hay sub


									 $query = "select
													id,
													nombre,
													(
														select
															count(*)
														from
															prod_sub_subcategoria
														where sub_id = c.id
													) as sub,
													(
														select
															count(*)
														from
															producto
														where cat_nivel = 2 and cat_id = '$CATID'
													) as art
												from
													prod_subcategoria c where cat_id = '$CATID' order by orden";

									  $cat2 = $cat->estructura_db($query);

									  for($c2=0;$c2<count($cat2);$c2++){ $NIVEL = 2; $CATID = $cat2[$c2]['id']; ?>
									   <li><h3><?php echo utf8_encode($cat2[$c2]['nombre']); ?></h3>
									   <ul class="menu-3">
                                       <?php if($cat2[$c2]['sub']>0){ ///si hay sub sub

									   				$query = "select id,nombre from prod_sub_subcategoria c where sub_id = '$CATID' order by orden";

									 				 $cat3 = $cat->estructura_db($query);
													 for($c3=0;$c3<count($cat3);$c3++){ $NIVEL = 3; $CATID = $cat3[$c3]['id']; ?>
													 <li><h4><?php echo utf8_encode($cat3[$c3]['nombre']); ?></h4>
									   				 <ul class="menu-4">
                                                     <?php include('SVG-barra-productos.php'); ///listado 3 ?>
                                                     </ul>
                                       				 </li>
									   				 <?php }////fin cat3 ?>

									  <?
									   }else{ include('SVG-barra-productos.php'); }///listado 2   ?>
                                       </ul>
                                       </li>
                                   <?php }////fin cat2 ?>

							<?


								}else{ include('SVG-barra-productos.php'); }///listado articulos cat 1 }

							?>


                        </ul>
                    <?php } ?>
				</li>

 <?php } ////fin cat1
 	// Articulos

		$query = "select
					id,
					nombre,
					(
						select
							count(*)
						from
							cont_subcategoria where cat_id = c.id
					) as sub,
					(
						select
							count(*)
						from
							articulo
						where
							cat_nivel = 1 and cat_id = c.id
					) as art
				 from cont_categoria c
				 $extra1 and c.id<>1
				 order by orden";

		$cat1 = $cat->estructura_db($query);

	 for($c1=0;$c1<count($cat1);$c1++){ $NIVEL = 1; $CATID = $cat1[$c1]['id']; ?>

				<li><h2><?php echo utf8_encode($cat1[$c1]['nombre']); ?></h2>
                	<?php if(($cat1[$c1]['sub']+$cat1[$c1]['art']) > 0){ ?>
                        <ul class="menu-2">
                            <?php if($cat1[$c1]['sub']>0){ ///si hay sub


									 $query = "select
													id,
													nombre,
													(
														select
															count(*)
														from
															cont_sub_subcategoria
														where sub_id = c.id
													) as sub,
													(
														select
															count(*)
														from
															articulo
														where cat_nivel = 2 and cat_id = '$CATID'
													) as art
												from
													cont_subcategoria c where cat_id = '$CATID' order by orden";

									  $cat2 = $cat->estructura_db($query);

									  for($c2=0;$c2<count($cat2);$c2++){ $NIVEL = 2; $CATID = $cat2[$c2]['id']; ?>
									   <li><h3><?php echo utf8_encode($cat2[$c2]['nombre']); ?></h3>
									   <ul class="menu-3">
                                       <?php if($cat2[$c2]['sub']>0){ ///si hay sub sub

									   				$query = "select id,nombre from cont_sub_subcategoria c where sub_id = '$CATID' order by orden";

									 				 $cat3 = $cat->estructura_db($query);
													 for($c3=0;$c3<count($cat3);$c3++){ $NIVEL = 3; $CATID = $cat3[$c3]['id']; ?>
													 <li><h4><?php echo utf8_encode($cat3[$c3]['nombre']); ?></h4>
									   				 <ul class="menu-4">
                                                     <?php include('SVG-barra-articulos.php'); ///listado 3 ?>
                                                     </ul>
                                       				 </li>
									   				 <?php }////fin cat3 ?>

									  <?
									   }else{ include('SVG-barra-articulos.php'); }///listado 2   ?>
                                       </ul>
                                       </li>
                                   <?php }////fin cat2 ?>

							<?


								}else{ include('SVG-barra-articulos.php'); }///listado articulos cat 1 }

							?>


                        </ul>
                    <?php } ?>
				</li>
<?
	 }

 ?>

  </ul>
				</div>

<?php include ("SVG-redessociales.php")?>
<?php include ("SVG-twitter.php")?>
